<?php


namespace App\Utils\Logic\lak;

use App\Utils\Lib\MyEs;
class EsSearchLogic
{
    public static function EsSearch($name,$field,$from,$size){
        $es = new MyEs();

        //做高亮检索
        $params = [
            'index' => $es->index,
            'body' => [
                'query' => [
                    'match' => [
                        $field => $name
                    ]
                ],
                //高亮
                'highlight'=>[
                    'pre_tags'=>[
                        "<span style='color: red'>",
                    ],
                    'post_tags'=>["</span>"],
                    'fields'=>[
                        $field =>new \stdClass()
                    ]
                ],
                // 联想搜索
                'suggest' => [
                    'text' => $name,
                    'my-suggestion' => [
                        'term' => [
                            'field' => $field,
                            'suggest_mode' => 'popular',
                        ],
                    ],
                ],
            ],
            'from'=>$from,//偏移量
            'size'=>$size,//每页显示条数
        ];

//        // 全量搜索
//            $params = [
//                'index' => $es->index,
//                'type'=>$es->type,
//                'body' => [
//                    'query' => [
//                        'match_all' => new \stdClass()
//                    ]
//                ]
//            ];

        $results = $es->client->search($params);
        //提取出Es中想要的数据
        $data = $results['suggest']['my-suggestion'][0]['options'];
        $data = $results['hits']['hits'];
        //dd($data);die();
        return $data;
    }

}
